package com.mingqijia.gassafety.webserver.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.gap.basic.exception.BizException;
import com.mingqijia.gassafety.db.entity.Consumer;
import com.mingqijia.gassafety.db.entity.Dtu;
import com.mingqijia.gassafety.db.mapper.ConsumerMapper;
import com.mingqijia.gassafety.db.mapper.DtuMapper;
import com.mingqijia.gassafety.shared.dto.UserFuncCodePermissionDTO;
import com.mingqijia.gassafety.shared.utils.ApplicationUtils;
import com.mingqijia.gassafety.webserver.service.OrganizationalDepartmentService;
import com.mingqijia.gassafety.webserver.service.UserPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class UserPermissionServiceImpl implements UserPermissionService {
    @Autowired
    OrganizationalDepartmentService organizationalDepartmentService;

    @Autowired
    ConsumerMapper consumerMapper;

    @Autowired
    DtuMapper dtuMapper;

    @Override
    public UserFuncCodePermissionDTO getUserDepartPermission(List<String> spIds) {
        String spId;
        String userId;
        try {
            spId = ApplicationUtils.getHeaderSpId();
            userId = ApplicationUtils.getUserId();
        }catch (Exception e){
            log.error("getUserDepartPermission error:",e);
            return new UserFuncCodePermissionDTO();
        }
        return getUserDepartPermission(spId, userId);
    }

    @Override
    public UserFuncCodePermissionDTO getUserDepartPermission(String spId, String userId) {
        UserFuncCodePermissionDTO userFuncCodePermission = organizationalDepartmentService.getArea(spId, userId);
        if (null == userFuncCodePermission || null == userFuncCodePermission.getAuthority()) {
            throw new BizException("未配置该账号的数据权限,请先配置");
        }
        int authority = userFuncCodePermission.getAuthority();
        List<String> funcCodes = userFuncCodePermission.getFuncCodes();
        if (authority == 1 && (null == funcCodes || funcCodes.isEmpty())) {
            throw new BizException("该账号没有任何区域数据权限");
        }
        return userFuncCodePermission;
    }

    @Override
    public void checkDepartPermission(Integer consumerId, String spId, String userId) {
        UserFuncCodePermissionDTO userFuncCodePermission = getUserDepartPermission(spId, userId);
        if (0 != userFuncCodePermission.getAuthority()) {//拥有所有权限
            List<String> funcCodes = userFuncCodePermission.getFuncCodes();
            Consumer consumer = consumerMapper.selectById(consumerId);
            if (StringUtils.isEmpty(consumer.getCitycode()) && StringUtils.isEmpty(consumer.getDistrictcode()) &&
                    StringUtils.isEmpty(consumer.getTowncode())) {
                log.info("功能区信息为空，不在数据权限中，不能修改");
                throw new BizException("没有修改该数据的权限");
            } else if (StringUtils.isNotEmpty(consumer.getTowncode()) && !funcCodes.contains(consumer.getTowncode())) {
                throw new BizException("没有修改该数据的权限");
            } else if (StringUtils.isNotEmpty(consumer.getDistrictcode()) && !funcCodes.contains(consumer.getDistrictcode())) {
                throw new BizException("没有修改该数据的权限");
            } else if (StringUtils.isNotEmpty(consumer.getCitycode()) && !funcCodes.contains(consumer.getCitycode())) {
                throw new BizException("没有修改该数据的权限");
            }
        }
    }

    @Override
    public void checkDepartPermission(String imei, String spId, String userId) {
        Dtu dtu = dtuMapper.selectById(imei);
        if (ObjectUtil.isNotEmpty(dtu) && null != dtu.getConsumerId() && dtu.getConsumerId() > 0) {
            checkDepartPermission(dtu.getConsumerId(), spId, userId);
        }
    }

    @Override
    public List<Integer> getDepartConsumerIds(String spId, String userId) {
        UserFuncCodePermissionDTO userFuncCodePermission = getUserDepartPermission(spId, userId);
        if (null == userFuncCodePermission.getFuncCodes() || userFuncCodePermission.getFuncCodes().isEmpty()) {
            return Collections.emptyList();
        }
        List<Integer> consumerIds = consumerMapper.getDepartConsumerIds(spId, userFuncCodePermission.getFuncCodes());
        return consumerIds;
    }

    @Override
    public List<String> getDepartDeviceIds(String spId, String userId) {
        List<Integer> consumerIds = getDepartConsumerIds(spId,userId);
        List<String> result = new ArrayList<>();
        if( null==consumerIds || consumerIds.isEmpty() ){
            return result;
        }
        result.add("0");
        List<String> imeis = dtuMapper.getDepartConsumerDeviceIds(spId,consumerIds);
        result.addAll(imeis);
        return result;
    }
}
